/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.apexlamps;

import java.io.IOException;
import java.util.ArrayList;
import jxl.write.WriteException;
import pricecomparison.PriceComparisonView;
import pricecomparison.websitereader.WebsiteReader;

/**
 *
 * @author Jim Simon
 */
public class AL_ItemReader extends AL_HomePageReader {
    String URL = super.URL;
    WebsiteReader reader;
    
    public AL_ItemReader(String url) throws java.net.MalformedURLException, java.io.IOException {
        URL = url;
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
    }
    
    public void beginSearch() throws java.io.IOException, jxl.write.WriteException {
        String name, price;
        
        // Get name and price
        try {
            name = getItemName();
            price = getPrice();
            reader.close();
        } catch (java.io.IOException e) {
            throw e;
        }

        // Print debug info
        if (reader.DEBUG) {
            System.out.println("--- Name: " + name);
            System.out.println("--- Price: " + price);
        }

        // Put item details into an array list.
        ArrayList<String> items = new ArrayList();
        items.add(name);
        items.add(price);
        try {
            PriceComparisonView.excelWriter.writeItem(items, PriceComparisonView.excelWriter.MFER_APEXLAMPS);
        } catch (jxl.write.WriteException e) {
            throw e;
        }
    }
    
    public String getItemName() throws java.io.IOException {
        String line = reader.readLine();
        String key = "productName";
        
        try {
            // Find key
            while (!line.contains(key))
                line = reader.readLine();
            line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        line = line.replace("&nbsp; ", "");
        
        // Find end portion
        key = "</div>";
        int pos = line.indexOf(key);
        line = line.substring(0, pos);
        
        return line.trim();
    }
    
    public String getPrice() throws java.io.IOException{
        String line;
        String key = "USD";
        try {
            // Find key
            line = reader.readLine();
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        line = line.substring(line.indexOf(key) + key.length());
        
        // Find beginning portion
        key = "$";
        int pos = line.indexOf(key);
        line = line.substring(pos);   
        
        // Find end portion
        key = "</span>";
        pos = line.indexOf(key);
        line = line.substring(0, pos-1);
        
        return line;
    }
}
